package com.eedi.framework.notify.dal.mapper;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.mybatis.core.dataobject.BaseDO;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.eedi.framework.notify.controller.sys.vo.SysNoticePageReq;
import com.eedi.framework.notify.controller.sys.vo.SysNoticeResp;
import com.eedi.framework.notify.dal.dataobject.SysNoticeDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;

@Mapper
public interface NoticeMapper extends BaseMapperX<SysNoticeDO> {

    default PageResult<SysNoticeResp> selectPage(SysNoticePageReq reqVO) {
        PageResult<SysNoticeDO> result = selectPage(reqVO, new LambdaQueryWrapperX<SysNoticeDO>()
                .likeIfPresent(SysNoticeDO::getSysNoticeTitle, reqVO.getSysNoticeTitle())
                .eqIfPresent(SysNoticeDO::getSysNoticeStatus, reqVO.getSysNoticeStatus())
                .betweenIfPresent(BaseDO::getCreateTime,reqVO.getStartTime(),reqVO.getEndTime())
                .orderByDesc(SysNoticeDO::getSysNoticeSort)
                .orderByDesc(BaseDO::getCreateTime));
        return BeanUtils.toBean(result, SysNoticeResp.class);
    }

    default void  updateStatusById(String sysNoticeId, CommonStatusEnum statusEnum){
        if (StrUtil.isBlank(sysNoticeId)){
            return;
        }
        SysNoticeDO sysNoticeDO=new SysNoticeDO();
        sysNoticeDO.setSysNoticeId(sysNoticeId)
                .setSysNoticeStatus(statusEnum);
        updateById(sysNoticeDO);
    }

    default List<SysNoticeResp> selectNotInIds(Collection<String> sysNoticeIds){
        LambdaQueryWrapperX<SysNoticeDO> wrapperX = new LambdaQueryWrapperX<>();
        if (CollUtil.isNotEmpty(sysNoticeIds)){
            wrapperX.notIn(SysNoticeDO::getSysNoticeId, sysNoticeIds);
        }
        List<SysNoticeDO> sysNotices = selectList(wrapperX);
        return BeanUtils.toBean(sysNotices, SysNoticeResp.class);
    }
}
